import {Desk} from "./disPlayConfig";
import {modules} from "../../engines/gameEngine";

const {ccclass, property} = cc._decorator;

@ccclass
export default class disItem extends cc.Component {

    @property(cc.Label)
    lbName: cc.Label = null;
    @property(cc.Sprite)
    spIcon: cc.Sprite = null;

    // LIFE-CYCLE CALLBACKS:

    protected onLoad() {

    }

    protected start() {
        this.initView();
    }

    private m_data: Desk.DisplayItem = null;
    private m_parentCallback: () => void = null;

    setId(id: string) {
        this.m_data = <Desk.DisplayItem>Desk.getDisPlayItemList(id);
    }

    initView() {
        if (this.m_data == null) {
            console.error('#29152544 未初始化数据', disItem);
            return;
        }
        if (this.m_data && this.m_data.icon) {
            modules().uiControlModule.getFrameBundle().load(
                `preload/hallIcon/${this.m_data.icon}`,
                cc.SpriteFrame,
                (error, assets: cc.SpriteFrame) => {
                    if (error) {
                        console.error('#405511 错误的icon', this.m_data.icon);
                        return;
                    }
                    this.spIcon.spriteFrame = assets;
                });
        }
        //
        this.lbName.string = this.m_data.name;
    }

    addParentClick(callback?: () => void) {
        this.m_parentCallback = callback;
    }

    onBtnClick(event, custom: string) {
        this.m_parentCallback && this.m_parentCallback();
        this.m_data.onClickEvent && this.m_data.onClickEvent(event, custom);
    }

    // update (dt) {}
}
